package com.guigui.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @author ：xin
 * @date ：Created in 2021/7/25 20:46
 * @description：
 */
@Configuration
public class RenenCorsConfiguration {

    @Bean
    public CorsWebFilter corsWebFilter (){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();

        //跨域配置
        //允许那些请求头
        corsConfiguration.addAllowedHeader("*");
        //允许那些请求方法访问
        corsConfiguration.addAllowedMethod("*");
        //允许那些来源访问
        corsConfiguration.addAllowedOrigin("*");
        //是否允许携带cookie信息
        corsConfiguration.setAllowCredentials(true);

        //表示任意路径都需要跨域配置
        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(source);
    }
}
